// page/index/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region:['江苏省','南京市','秦淮区'],
    customItem: '全部',
    nows:'',
    updata:'',
    lifeStyle:'',
    location:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getloca()
   
  },
  
  // 改变picker
  change: function(e){
    console.log(111)
    // --
    this.setData({
      region: e.detail.value,
      flag:false
    })
    // 天气情况
   this.getRq();
  //  生活指数
    this.getls();
  },

  //网络请求函数--天气
   getRq:function(e){
     var httpUrl = 'https://free-api.heweather.net/s6/weather/now?'
     var that = this
       wx.request({
         url: httpUrl, //仅为示例，并非真实的接口地址
         data: {
           location: that.data.region[1],
           key: 'e3f16084c5bb4aeda37a6b5b4c7c0032'
         },
         success(res) {
           that.setData({
             nows: res.data.HeWeather6[0].now,
             updata: res.data.HeWeather6[0].update
           })
         }
       })
   },

//生活指数
  getls: function (e) {
    var httpUrl = 'https://free-api.heweather.net/s6/weather/lifestyle?'
    var that = this
      wx.request({
        url: httpUrl, //仅为示例，并非真实的接口地址
        data: {
          location: that.data.region[1],
          key: 'e3f16084c5bb4aeda37a6b5b4c7c0032'
        },
        success(res) {
          that.setData({
            lifeStyle: res.data.HeWeather6[0].lifestyle
          })
        }
      })
  
  },

  // 定位，获取当前的定位
  getloca:function(){
    var that = this
    wx.getLocation({
      type: 'gcj02',
      success(res) {
        // console.log(res)
        // 经纬度
        var weidu = res.latitude
        var jingdu = res.longitude
        var loca = jingdu + ',' + weidu
        var httpUrl = 'https://free-api.heweather.net/s6/weather/now?'
        wx.request({
          url:httpUrl,
          data:{
            location: loca,
            key: 'e3f16084c5bb4aeda37a6b5b4c7c0032'
          },
          success:(res) => {
            // console.log(res.data.HeWeather6[0])
            let result = res.data.HeWeather6[0].basic
            that.setData({
              
              region:[result.admin_area, result.parent_city, result.location]
            })
          }
        })

      }
    })
    //调用天气
    that.getRq()
    //调用生活指数
    that.getls()
  }
})